home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 2: Applications / Linux Cubed Series 2 - Applications.iso / utils / console / splitvt-.000 / splitvt- / splitvt-1.6.1 / vtmouse.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-12-14  |  6.0 KB  |  250 lines

  1.  
  2. /* This is a program to show how mouse events can be handled by programs
  3.    running under an xterm window.
  4.  
  5.         -Sam Lantinga        5/11/94
  6. */
  7.  
  8. #define SPLITVT_SOURCE
  9.  
  10. #include <stdio.h>
  11. #include "vtmouse.h"
  12.  
  13. #ifdef MAIN
  14. void event_loop()
  15. {
  16.     int c, event_flag;
  17.     struct event X_event;
  18.  
  19.     while ( (c=event_getc(&X_event)) != EOF ) {
  20.         if ( X_event.happening ) {
  21.             printf("\r\n************\r\n");
  22.             printf("Mouse Event: 0x%x\r\n", X_event.button_state);
  23.             printf("\tX coordinate: %d\r\n", X_event.x);
  24.             printf("\tY coordinate: %d\r\n", X_event.y);
  25.             /* One example of getting the mouse state */
  26.             printf("\tX Event: ");
  27.             switch(X_event.button_state&BUTTON_MASK) {
  28.                 case RELEASE:    printf("Button was released!\r\n");
  29.                         break;
  30.                 default:    printf("Button %d was pressed!\r\n", 
  31.                             (X_event.button_state&BUTTON_MASK)+1);
  32.                         break;
  33.             }
  34.             /* Another example of getting the mouse state */
  35.             printf("\tMouse %s buttons <%c|%c|%c>\r\n",
  36.                 (BUTTON_ISSET(X_event, RELEASE) ? "released" : "pressed"),
  37.                 (BUTTON_ISSET(X_event, BUTTON_1) ? 'X' : ' '),
  38.                 (BUTTON_ISSET(X_event, BUTTON_2) ? 'X' : ' '),
  39.                 (BUTTON_ISSET(X_event, BUTTON_3) ? 'X' : ' '));
  40. #ifdef REPORT_SELECTION
  41.             if ( (X_event.button_state&SELECTED) == SELECTED )
  42.                 printf("Selection: Start x=%d, y=%d; End x=%d, y=%d\r\n",
  43.                         X_event.selection.begin_col,
  44.                         X_event.selection.begin_row,
  45.                         X_event.selection.end_col,
  46.                         X_event.selection.end_row);
  47. #endif /* REPORT_SELECTION */
  48.             printf("************\r\n");
  49.         } else {
  50.             if ( c == '\r' ) {
  51.                 putc('\r', stdout);
  52.                 putc('\n', stdout);
  53.             } else if ( c < ' ' ) {
  54.                 putc('^', stdout);
  55.                 putc(c+'@', stdout);
  56.             } else if ( c == 'Q' )
  57.                 return;
  58.             else
  59.                 putc(c, stdout);
  60.         }
  61.     }
  62. }
  63.  
  64. int main(int argc, char *argv[])
  65. {
  66.     if ( event_init(stdin, stdout, NULL) < 0 ) {
  67.         printf("Not running under an xterm.\n");
  68.         exit(2);
  69.     }
  70.     system("stty -echo raw");
  71.  
  72.     event_loop();
  73.  
  74.     system("stty -raw echo");
  75.     event_quit();
  76.  
  77.     exit(0);
  78. }
  79. #endif
  80.  
  81. /* I/O streams default to stdin and stdout. */
  82. FILE *xt_input=stdin, *xt_output=stdout;
  83. static int have_xterm=0;
  84. static int set_title=0;
  85. int terminal_input=0;        /* Is there pending terminal input? */
  86.  
  87. int event_init(input, output, titlebar)
  88. FILE *input, *output;
  89. char *titlebar;
  90. {
  91.     char *termtype;
  92.  
  93.     /* A program can assume that input is unbuffered after this routine */
  94.     setbuf(input, NULL);
  95.     xt_input=input;
  96.     xt_output=output;
  97.  
  98.     /* Check for xterm terminal type */
  99.     if ( (termtype=(char *)getenv("TERM")) && strcmp(termtype, "xterm") == 0 ) {
  100. #ifdef REPORT_SELECTION
  101.         fprintf(xt_output, "\033[?1001h");
  102. #else
  103.         fprintf(xt_output, "\033[?1000h");
  104. #endif /* REPORT_SELECTION */
  105.         fflush(xt_output);
  106.         have_xterm=1;
  107.         if ( titlebar ) {
  108.             fprintf(xt_output, "\033]0;%s\07", titlebar);
  109.             set_title=1;
  110.             fflush(xt_output);
  111.         }
  112.     } else {
  113.         return(-1);
  114.     }
  115.     return(0);
  116. }
  117.  
  118. int event_getc(X_event)
  119. struct event *X_event;
  120. {
  121. #ifdef REPORT_SELECTION
  122.     static int last_row, last_col;
  123. #endif
  124.     int c;
  125.     static char prefix[8], *next;
  126. #ifdef SPLITVT_SOURCE
  127.     extern struct physical physical;
  128.     window *thiswin;
  129. #endif
  130.  
  131.     X_event->happening=0;
  132.  
  133.     if ( have_xterm ) {
  134.         if ( terminal_input == 0 ) { /* Brand new input */
  135.             strcpy(prefix, "\033[");    /* Sequence prefix */
  136.             next=prefix;
  137.             while ( (c=getc(xt_input)) != EOF ) {
  138.                 if ( *next ) {
  139.                     if ( c != *next ) {
  140.                         *next = c;
  141.                         terminal_input=(next-prefix);
  142.                         next=prefix;
  143.                         return(*(next++));
  144.                     } else
  145.                         ++next;
  146.                 } else
  147.                     break;
  148.             }
  149.         } else { /* We already have input to return */
  150.             --terminal_input;
  151.             return(*(next++));
  152.         }
  153.  
  154.         /* If we got here, we got prefix + c */
  155.         switch (c) {
  156.             case 'M':    /* ^[[M%d%d%d  (assume getc() is ok) */
  157.                     X_event->button_state =
  158.                             (getc(xt_input)-' ');
  159.                     X_event->y=(getc(xt_input)-' ');
  160.                     last_col=X_event->y;
  161.                     X_event->x=(getc(xt_input)-' ');
  162.                     last_row=X_event->x;
  163. #ifdef REPORT_SELECTION
  164.                     if (BUTTON_ISSET((*X_event), BUTTON_1))
  165.                     { /* Tell xterm to start selection */
  166. #ifdef SPLITVT_SOURCE
  167.             if ( X_event->x < (physical.subwins[LOWER])->row_offset )
  168.             thiswin=physical.subwins[UPPER];
  169.         else
  170.             thiswin=physical.subwins[LOWER];
  171.         fprintf(xt_output, "\033[1;%d;%d;%d;%dT",
  172.                     /* startcol, startrow, */
  173.                     X_event->y, X_event->x,
  174.                     /* first row */
  175.                     1 + thiswin->row_offset,
  176.                     /* last row */
  177.                     thiswin->rows + thiswin->row_offset);
  178. #else
  179.                         fprintf(xt_output, 
  180.                             "\033[1;%d;%d;%d;%dT",
  181.                         /* startcol, startrow, */
  182.                             X_event->y, X_event->x,
  183.                         /* firstrow, lastrow */
  184.                             1, 24);
  185. #endif /* SPLITVT_SOURCE */
  186.                         fflush(xt_output);
  187.                     }
  188. #endif /* REPORT_SELECTION */
  189.                     X_event->happening=1;
  190.                     break;
  191. #ifdef REPORT_SELECTION
  192.             case 't':    /* ^[[t%d%d */
  193.                     X_event->button_state = 
  194.                             (RELEASE|SELECTED);
  195.                     X_event->selection.begin_row=last_row;
  196.                     X_event->selection.begin_col=last_col;
  197.                     X_event->selection.end_col =
  198.                             (getc(xt_input)-' ');
  199.                     X_event->selection.end_row =
  200.                             (getc(xt_input)-' ');
  201.                     X_event->happening=1;
  202.                     break;
  203.             case 'T':    /* ^[[T%d%d%d%d%d%d */
  204.                     X_event->y=(getc(xt_input)-' ');
  205.                     X_event->x=(getc(xt_input)-' ');
  206.                     X_event->button_state = 
  207.                             (RELEASE|SELECTED);
  208.                     X_event->selection.begin_col =
  209.                             (getc(xt_input)-' ');
  210.                     X_event->selection.begin_row =
  211.                             (getc(xt_input)-' ');
  212.                     X_event->selection.end_col =
  213.                             (getc(xt_input)-' ');
  214.                     X_event->selection.end_row =
  215.                             (getc(xt_input)-' ');
  216.                     X_event->happening=1;
  217.                     break;
  218. #endif /* REPORT_SELECTION */
  219.             case EOF:    /* We got EOF, return EOF */
  220.                     return(EOF);
  221.                     break;
  222.             default:    /* It's not an event sequence */
  223.                     *next = c;
  224.                     terminal_input=(next-prefix);
  225.                     next=prefix;
  226.                     return(*(next++));
  227.                     break;
  228.         }
  229.     } else
  230.         return(getc(xt_input));
  231.     return(0);
  232. }
  233.  
  234. void event_quit()
  235. {
  236.     if ( have_xterm ) {
  237. #ifdef REPORT_SELECTION
  238.         fprintf(xt_output, "\033[?1001l");
  239. #else
  240.         fprintf(xt_output, "\033[?1000l");
  241. #endif /* REPORT_SELECTION */
  242.         fflush(xt_output);
  243.         if ( set_title ) {
  244.             fprintf(xt_output, "\033]0;%s\07", "xterm");
  245.             fflush(xt_output);
  246.         }
  247.     }
  248. }
  249.  
  250.